//
//  DLPickerView.h
//  DLPickerView
//
//  Created by 伍明鹏 on 2019/12/12.
//

#import "DLPickerView.h"
#import "NSDate+Calendar.h"

#define kPickerSize self.datePicker.frame.size
#define bottom_height (kDevice_Is_iPhoneX ? 64.f : 40.f)


typedef void(^doneBlock)(NSDate *);
typedef void(^unDatedoneBlock)(NSArray<ResponseSelectionOption *>*);

@implementation ResponseSelectionOption
@end

@interface DLPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource,UIGestureRecognizerDelegate> {
    //日期存储数组
    NSMutableArray *_yearArray;
    NSMutableArray *_monthArray;
    NSMutableArray *_dayArray;
    NSMutableArray *_hourArray;
    NSMutableArray *_minuteArray;
    NSString *_dateFormatter;
    //记录位置
    NSInteger yearIndex;
    NSInteger monthIndex;
    NSInteger dayIndex;
    NSInteger hourIndex;
    NSInteger minuteIndex;
    
    NSInteger preRow;//日期滚动到的位置
    NSDate *_startDate;//选中时间
    NSInteger _maxYear;//最大时间年限
    NSInteger _minYear;//最小时间年限
    
    NSDate *_minDate;
    NSDate *_maxDate;
    NSString *_yearstring;
    NSString *_monthstring;
    NSString *_daystring;
    NSString *_hourstring;
    NSString *_minitstring;
}
@property (weak, nonatomic) IBOutlet UIView *buttomView;//选择器试图
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;//确定按钮
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (weak, nonatomic) IBOutlet UILabel *statusTitle;//状态标题
@property (weak, nonatomic) IBOutlet UIButton *cancelTitle;//取消按钮
- (IBAction)doneAction:(UIButton *)btn;
- (IBAction)cancelAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *statusView;//取消确定栏

@property (weak, nonatomic) IBOutlet UIView *longTimeView;//长期View
@property (weak, nonatomic) IBOutlet UIButton *longTimeButton;//长期按钮
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;


@property (nonatomic,strong)NSArray <NSArray<ResponseSelectionOption*>*>*unDateArray;//非日期数据
@property (nonatomic,strong)UIPickerView *datePicker;
@property (nonatomic,strong) NSDate *scrollToDate;//滚到指定日期
@property (nonatomic,strong) NSMutableArray <ResponseSelectionOption*>*scrollToSelected;//非日期滚到指定位置
@property (nonatomic,copy)doneBlock doneBlock;//日期回调block
@property (nonatomic,copy)unDatedoneBlock unDatedoneBlock;//非日期回调block
@property (nonatomic,assign)DLDateStyle datePickerStyle;
@property (nonatomic,strong)NSMutableArray *unDateSelectArr;//非日期选择数据
@property (nonatomic,assign)BOOL isShowSpeciDate;


@end

@implementation DLPickerView
/**
 默认滚动到当前时间
 */
-(instancetype)initWithDateStyle:(DLDateStyle)datePickerStyle maxYear:(NSInteger)maxYear minYear:(NSInteger)minYear completeBlock:(void(^)(NSDate *))completeBlock {
    self = [super init];
    if (self) {
        NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[DLPickerView class]] pathForResource:@"PickerViewBundle" ofType:@"bundle"]];
        self = [[bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
        
        self.datePickerStyle = datePickerStyle;
        _maxYear = maxYear > 0 ? maxYear : 2099;
        _minYear = minYear >=0 && minYear < maxYear ? minYear : 1900;
        
        NSString *maxLimitStr = [NSString stringWithFormat:@"%d-12-31 23:59",maxYear];
        self.maxLimitDate = [NSDate date:maxLimitStr WithFormat:@"yyyy-MM-dd HH:mm"];
        if (!self.scrollToDate) {
            self.scrollToDate = self.maxLimitDate;
        }
        NSString *minLimitStr = [NSString stringWithFormat:@"%d-01-01 00:00",minYear];
        self.minLimitDate = [NSDate date:minLimitStr WithFormat:@"yyyy-MM-dd HH:mm"];

        
        if (self.pickerViewTitle && self.pickerViewTitle.length > 0) {
            self.statusTitle.text = self.pickerViewTitle;
        }
        
        switch (datePickerStyle) {
            case DateStyleShowYearMonthDayHourMinute:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
            case DateStyleShowMonthDayHourMinute:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
            case DateStyleShowYearMonthDay:
                _dateFormatter = @"yyyy-MM-dd";
                break;
            case DateStyleShowYearMonth:
                _dateFormatter = @"yyyy-MM";
                break;
            case DateStyleShowMonthDay:
                _dateFormatter = @"yyyy-MM-dd";
                break;
            case DateStyleShowHourMinute:
                _dateFormatter = @"HH:mm";
                break;
            case DateStyleShowYear:
                _dateFormatter = @"yyyy";
                break;
            case DateStyleShowMonth:
                _dateFormatter = @"MM";
                break;
            case DateStyleShowDayHourMinute:
                _dateFormatter = @"dd HH:mm";
                break;
            default:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
        }
        
        [self setupUI];
        [self defaultConfig];
        
        if (completeBlock) {
            self.doneBlock = ^(NSDate *selectDate) {
                completeBlock(selectDate);
            };
        }
    }
    return self;
}

/**
 滚动到指定的的日期
 */
-(instancetype)initWithDateStyle:(DLDateStyle)datePickerStyle maxYear:(NSInteger)maxYear minYear:(NSInteger)minYear scrollToDate:(NSDate *)scrollToDate completeBlock:(void(^)(NSDate *))completeBlock {
    self = [super init];
    if (self) {
        NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[DLPickerView class]] pathForResource:@"DGGUISpaceSDK" ofType:@"bundle"]];
        self = [[bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
        self.datePickerStyle = datePickerStyle;
        self.scrollToDate = scrollToDate;
        if (minYear > maxYear) return nil;
        _maxYear = maxYear > 0 ? maxYear : 2099;
        _minYear = minYear >=0 && minYear < maxYear ? minYear : 1900;
    
        NSString *maxLimitStr = [NSString stringWithFormat:@"%d-12-31 23:59",maxYear];
        self.maxLimitDate = [NSDate date:maxLimitStr WithFormat:@"yyyy-MM-dd HH:mm"];
        if (!self.scrollToDate) {
            self.scrollToDate = self.maxLimitDate;
        }
        NSString *minLimitStr = [NSString stringWithFormat:@"%d-01-01 00:00",minYear];
        self.minLimitDate = [NSDate date:minLimitStr WithFormat:@"yyyy-MM-dd HH:mm"];
        
        
        if (self.pickerViewTitle && self.pickerViewTitle.length > 0) {
            self.statusTitle.text = self.pickerViewTitle;
        }
        switch (datePickerStyle) {
            case DateStyleShowYearMonthDayHourMinute:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
            case DateStyleShowMonthDayHourMinute:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
            case DateStyleShowYearMonthDay:
                _dateFormatter = @"yyyy-MM-dd";
                break;
            case DateStyleShowYearMonth:
                _dateFormatter = @"yyyy-MM";
                break;
            case DateStyleShowMonthDay:
                _dateFormatter = @"yyyy-MM-dd";
                break;
            case DateStyleShowHourMinute:
                _dateFormatter = @"HH:mm";
                break;
            case DateStyleShowYear:
                _dateFormatter = @"yyyy";
                break;
            case DateStyleShowMonth:
                _dateFormatter = @"MM";
                break;
            case DateStyleShowDayHourMinute:
                _dateFormatter = @"dd HH:mm";
                break;
            default:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
        }
                
        [self setupUI];
        [self defaultConfig];
        
        if (completeBlock) {
            self.doneBlock = ^(NSDate *selectDate) {
                completeBlock(selectDate);
            };
        }
    }
    return self;
}

/**
 从指定时间为开始时间/截止时间
 
 */
-(instancetype)initWithDateStyle:(DLDateStyle)datePickerStyle maxYearStr:(NSString *)maxYearStr minYearStr:(NSString *)minYearStr completeBlock:(void(^)(NSDate *))completeBlock {
    self = [super init];
    if (self) {
        NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[DLPickerView class]] pathForResource:@"PickerViewBundle" ofType:@"bundle"]];
        self = [[bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
        
        NSDate *maxYearDate = [NSDate date:maxYearStr WithFormat:@"yyyy-MM-dd"];
        NSDate *minYearDate = [NSDate date:minYearStr WithFormat:@"yyyy-MM-dd"];
        if (!maxYearDate || !minYearDate)  return nil;
        
        NSArray *maxYearArray = [maxYearStr componentsSeparatedByString:@"-"];
        NSArray *minYearArray = [minYearStr componentsSeparatedByString:@"-"];
        NSInteger maxYear = [[maxYearArray objectAtIndex:0] integerValue];
        NSInteger minYear = [[minYearArray objectAtIndex:0] integerValue];
        if (minYear > maxYear) return nil;
        self.isShowSpeciDate = YES;
        
        _minDate = minYearDate;
        _maxDate = maxYearDate;
        yearIndex = 0;
        monthIndex = 0;
        dayIndex = 0;
        hourIndex = 0;
        minuteIndex = 0;
        
        _yearArray = [[NSMutableArray alloc] init];
        _monthArray = [[NSMutableArray alloc] init];
        _dayArray = [[NSMutableArray alloc] init];
        _hourArray = [[NSMutableArray alloc] init];
        _minuteArray = [[NSMutableArray alloc] init];
        
        _yearstring = [NSString stringWithFormat:@"%d",[_minDate getYear]];
        _monthstring = [NSString stringWithFormat:@"%d",[_minDate getMonth]];
        _daystring = [NSString stringWithFormat:@"%d",[_minDate getDay]];
        _hourstring = [NSString stringWithFormat:@"%d",[_minDate getHour]];
        _minitstring = [NSString stringWithFormat:@"%d",[_minDate getMinute]];
        
        [self getYearsArray];
        [self getMonthsArray];
        [self getDaysArray];
        [self getHoursArray];
        [self getMinitsArray];
                
        self.datePickerStyle = datePickerStyle;
        _maxYear = maxYear > 0 ? maxYear : 2099;
        _minYear = minYear >=0 && minYear < maxYear ? minYear : 1900;
        
        NSString *maxLimitStr = [NSString stringWithFormat:@"%@ 23:59",maxYearStr];
        self.maxLimitDate = [NSDate date:maxLimitStr WithFormat:@"yyyy-MM-dd HH:mm"];
        NSString *minLimitStr = [NSString stringWithFormat:@"%@ 00:00",minYearStr];
        self.minLimitDate = [NSDate date:minLimitStr WithFormat:@"yyyy-MM-dd HH:mm"];
        
        if (!self.scrollToDate) {
            self.scrollToDate = self.minLimitDate;
        }
        
        if (self.pickerViewTitle && self.pickerViewTitle.length > 0) {
            self.statusTitle.text = self.pickerViewTitle;
        }
        
        switch (datePickerStyle) {
            case DateStyleShowYearMonthDayHourMinute:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
            case DateStyleShowMonthDayHourMinute:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
            case DateStyleShowYearMonthDay:
                _dateFormatter = @"yyyy-MM-dd";
                break;
            case DateStyleShowYearMonth:
                _dateFormatter = @"yyyy-MM";
                break;
            case DateStyleShowMonthDay:
                _dateFormatter = @"yyyy-MM-dd";
                break;
            case DateStyleShowHourMinute:
                _dateFormatter = @"HH:mm";
                break;
            case DateStyleShowYear:
                _dateFormatter = @"yyyy";
                break;
            case DateStyleShowMonth:
                _dateFormatter = @"MM";
                break;
            case DateStyleShowDayHourMinute:
                _dateFormatter = @"dd HH:mm";
                break;
            default:
                _dateFormatter = @"yyyy-MM-dd HH:mm";
                break;
        }
        
        [self setupUI];
        [self.datePicker selectRow:0 inComponent:0 animated:NO];
        yearIndex = 0;
        [self getMonthsArray];
//        [self.datePicker selectRow:0 inComponent:1 animated:NO];
//        [self.datePicker selectRow:0 inComponent:2 animated:NO];
//        [self defaultConfig];
        
        if (completeBlock) {
            self.doneBlock = ^(NSDate *selectDate) {
                completeBlock(selectDate);
            };
        }
    }
    return self;
}

-(instancetype)initWithData:(NSArray <NSArray <ResponseSelectionOption*>*>*)array completeBlock:(void(^)(NSArray<ResponseSelectionOption *> *))completeBlock
{
    self = [super init];
    if (self) {
        NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[DLPickerView class]] pathForResource:@"PickerViewBundle" ofType:@"bundle"]];
        self = [[bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
        if (self.pickerViewTitle && self.pickerViewTitle.length > 0) {
            self.statusTitle.text = self.pickerViewTitle;
        }
        self.unDateArray = array;
        self.datePickerStyle = unDateStyleShow;
        [self setupUI];
        [self defaultConfig];
        
        if (completeBlock) {
            self.unDatedoneBlock = ^(NSArray<ResponseSelectionOption *> *unDateSelectArr) {
                completeBlock(unDateSelectArr);
            };
        }
    }
    return self;
}

-(instancetype)initWithData:(NSArray <NSArray <ResponseSelectionOption*>*>*)array scrollToSelected:(NSArray<ResponseSelectionOption *> *)scrollToSelected completeBlock:(void(^)(NSArray<ResponseSelectionOption *> *))completeBlock{
    self = [super init];
    if (self) {
        NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[DLPickerView class]] pathForResource:@"PickerViewBundle" ofType:@"bundle"]];
        self = [[bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
        if (self.pickerViewTitle && self.pickerViewTitle.length > 0) {
            self.statusTitle.text = self.pickerViewTitle;
        }
        self.unDateArray = array;
        self.datePickerStyle = unDateStyleShow;
        [self.scrollToSelected removeAllObjects];
        [self.scrollToSelected addObjectsFromArray:scrollToSelected];
        [self setupUI];
        [self defaultConfig];
        
        if (completeBlock) {
            self.unDatedoneBlock = ^(NSArray<ResponseSelectionOption *> *unDateSelectArr) {
                completeBlock(unDateSelectArr);
            };
        }
    }
    return self;
}

-(void)setupUI {
    
    self.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    self.buttomView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    self.statusView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    self.longTimeView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    self.doneBtn.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    [self.cancelTitle setTitleColor:[[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x666666] darkColor:[UIColor colorWithHex:0x868686]] forState:UIControlStateNormal];
    
    //点击背景是否影藏
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    self.bottomConstraint.constant = -310;
    self.backgroundColor = CREATCOLOR(0, 0, 0, 0);
    [self layoutIfNeeded];
    self.longTimeView.hidden = YES;
    
    
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    [self.buttomView addSubview:self.datePicker];
    
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.buttomView);
        make.bottom.equalTo(self.buttomView.mas_bottom).offset(-bottom_height);
    }];
    self.topConstraint.constant = self.datePicker.frame.size.height+20;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.statusView.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight  cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.statusView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.statusView.layer.mask = maskLayer;
}

-(void)defaultConfig {
    
    if (!_scrollToDate) {
        _scrollToDate = [NSDate date];
    }
    
    
    //循环滚动时需要用到
    preRow = (self.scrollToDate.year-_minYear)*12+self.scrollToDate.month-1;
    
    //设置年月日时分数据
    _yearArray = [self setArray:_yearArray];
    _monthArray = [self setArray:_monthArray];
    _dayArray = [self setArray:_dayArray];
    _hourArray = [self setArray:_hourArray];
    _minuteArray = [self setArray:_minuteArray];
    
    for (int i=0; i<60; i++) {
        NSString *num = [NSString stringWithFormat:@"%02d",i];
        if (0<i && i<=12)
            [_monthArray addObject:num];
        if (i<24)
            [_hourArray addObject:num];
        [_minuteArray addObject:num];
    }
    for (NSInteger i=_minYear; i<=_maxYear; i++) {
        NSString *num = [NSString stringWithFormat:@"%ld",(long)i];
        [_yearArray addObject:num];
    }
    
    //最大最小限制
    if (!self.maxLimitDate) {
        self.maxLimitDate = [NSDate date:@"9999-12-31 23:59" WithFormat:@"yyyy-MM-dd HH:mm"];
    }
    //最小限制
    if (!self.minLimitDate) {
        self.minLimitDate = [NSDate date:@"0-01-01 00:00" WithFormat:@"yyyy-MM-dd HH:mm"];
    }
}

-(void)setDateLabelColor:(UIColor *)dateLabelColor {
    _dateLabelColor = dateLabelColor;
    _statusTitle.textColor = _dateLabelColor;
    for (id subView in self.buttomView.subviews) {
        if ([subView isKindOfClass:[UILabel class]]) {
            UILabel *label = subView;
            label.textColor = _dateLabelColor;
        }
    }
}


- (NSMutableArray *)setArray:(id)mutableArray
{
    if (mutableArray)
        [mutableArray removeAllObjects];
    else
        mutableArray = [NSMutableArray array];
    return mutableArray;
}

-(void)setYearLabelColor:(UIColor *)yearLabelColor {
    
}

#pragma mark - UIPickerViewDelegate,UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    switch (self.datePickerStyle) {
        case DateStyleShowYearMonthDayHourMinute:
            return 5;
        case DateStyleShowMonthDayHourMinute:
            return 4;
        case DateStyleShowYearMonthDay:
            return 3;
        case DateStyleShowYearMonth:
            return 2;
        case DateStyleShowMonthDay:
            return 2;
        case DateStyleShowHourMinute:
            return 2;
        case DateStyleShowYear:
            return 1;
        case DateStyleShowMonth:
            return 1;
        case DateStyleShowDayHourMinute:
            return 3;
        case unDateStyleShow:
            return self.unDateArray.count;
        default:
            return 0;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (self.isShowSpeciDate) {
        if (component == 0)
        {
            return _yearArray.count;
        }
        else if(component == 1)
        {
            return _monthArray.count;
        }else if (component == 2)
        {
            return _dayArray.count;
        }else if (component == 3)
        {
            return _hourArray.count;
        }else
        {
            return _minuteArray.count;
        }
    }else{
        if (self.datePickerStyle == unDateStyleShow) {
            if (![self.unDateArray[component] isKindOfClass:NSArray.class]) return 0;
            NSArray *subArr = self.unDateArray[component];
            return subArr.count;
        }else{
            NSArray *numberArr = [self getNumberOfRowsInComponent];
            return [numberArr[component] integerValue];
        }
    }
    
}

-(NSArray *)getNumberOfRowsInComponent {
    
    NSInteger yearNum = _yearArray.count;
    NSInteger monthNum = _monthArray.count;
    NSInteger dayNum = [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
    
    NSInteger dayNum2 = [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:1];//确保可以选到31日
    
    NSInteger hourNum = _hourArray.count;
    NSInteger minuteNUm = _minuteArray.count;
    
    NSInteger timeInterval = _maxYear - _minYear;
    
    switch (self.datePickerStyle) {
        case DateStyleShowYearMonthDayHourMinute:
            return @[@(yearNum),@(monthNum),@(dayNum),@(hourNum),@(minuteNUm)];
            break;
        case DateStyleShowMonthDayHourMinute:
            return @[@(monthNum*timeInterval),@(dayNum),@(hourNum),@(minuteNUm)];
            break;
        case DateStyleShowYearMonthDay:
            return @[@(yearNum),@(monthNum),@(dayNum)];
            break;
        case DateStyleShowYearMonth:
            return @[@(yearNum),@(monthNum)];
            break;
        case DateStyleShowMonthDay:
            return @[@(monthNum*timeInterval),@(dayNum),@(hourNum)];
            break;
        case DateStyleShowHourMinute:
            return @[@(hourNum),@(minuteNUm)];
            break;
        case DateStyleShowYear:
            return @[@(yearNum)];
            break;
        case DateStyleShowMonth:
            return @[@(monthNum)];
            break;
        case DateStyleShowDayHourMinute:
            return @[@(dayNum2),@(hourNum),@(minuteNUm)];
            break;
        default:
            return @[];
            break;
    }
    
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//
//    if (component == 0) {
//        return [_yearArray objectAtIndex:row];
//    }
//    else if (component == 1) {
//        return [_monthArray objectAtIndex:row];
//    }
//    else if (component == 2) {
//        return [_dayArray objectAtIndex:row];
//    }
//    else if (component == 3) {
//        return [_hourArray objectAtIndex:row];
//    }
//    else if (component == 4) {
//        return [_minuteArray objectAtIndex:row];
//    }
//    return @"";
//}
//
//-(UILabel *)labelForComponent:(NSInteger)component
//{
//    CGRect frame = CGRectMake(0, 0, self.bounds.size.width/5, 44);
//
//    UILabel *label = [[UILabel alloc] initWithFrame:frame];
//    label.textAlignment = NSTextAlignmentCenter;
//    label.backgroundColor = [UIColor clearColor];
//    label.userInteractionEnabled = NO;
//    label.textColor = [UIColor blackColor];
//    label.font = [UIFont systemFontOfSize:14];
//
//    return label;
//}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    if (self.isShowSpeciDate) {
//        UILabel *returnView = [self labelForComponent:component];
//        returnView.text = [self pickerView:pickerView titleForRow:row forComponent:component];
//        return returnView;
//    }else{
//
//    }
    
    //设置分割线的颜色
    if ([DLThemeAdapter shareInstance].isDark) {
        for(UIView *singleLine in pickerView.subviews)
            {
                if (singleLine.frame.size.height < 1)
                {
                    singleLine.backgroundColor = [UIColor colorWithHex:0xFF3C3C3D];
                    CGRect frame = singleLine.frame;
                    frame.origin.x = 20;
                    frame.size.width = SCREEN_WIDTH - 40;
                    frame.size.height = 0.5;
                    singleLine.frame = frame;
                }
            }
    }
    
    UILabel *customLabel = (UILabel *)view;
    if (!customLabel) {
        customLabel = [[UILabel alloc] init];
        customLabel.textAlignment = NSTextAlignmentCenter;
        [customLabel setFont:[UIFont systemFontOfSize:17]];
    }
    NSString *title;
    
    switch (self.datePickerStyle) {
        case DateStyleShowYearMonthDayHourMinute:
            if (component==0) {
                title = _yearArray[row];
            }
            if (component==1) {
                title = _monthArray[row];
            }
            if (component==2) {
                title = _dayArray[row];
            }
            if (component==3) {
                title = _hourArray[row];
            }
            if (component==4) {
                title = _minuteArray[row];
            }
            break;
        case DateStyleShowYearMonthDay:
            if (component==0) {
                title = _yearArray[row];
            }
            if (component==1) {
                title = _monthArray[row];
            }
            if (component==2) {
                title = _dayArray[row];
            }
            break;
        case DateStyleShowYearMonth:
            if (component==0) {
                title = _yearArray[row];
            }
            if (component==1) {
                title = _monthArray[row];
            }
            break;
        case DateStyleShowMonthDayHourMinute:
            if (component==0) {
                title = _monthArray[row%12];
            }
            if (component==1) {
                title = _dayArray[row];
            }
            if (component==2) {
                title = _hourArray[row];
            }
            if (component==3) {
                title = _minuteArray[row];
            }
            break;
        case DateStyleShowMonthDay:
            if (component==0) {
                title = _monthArray[row%12];
            }
            if (component==1) {
                title = _dayArray[row];
            }
            break;
        case DateStyleShowHourMinute:
            if (component==0) {
                title = _hourArray[row];
            }
            if (component==1) {
                title = _minuteArray[row];
            }
            break;
        case DateStyleShowYear:
            if (component==0) {
                title = _yearArray[row];
            }
            break;
        case DateStyleShowMonth:
            if (component==0) {
                title = _monthArray[row];
            }
            break;
        case DateStyleShowDayHourMinute:
            if (component==0) {
                title = _dayArray[row];
            }
            if (component==1) {
                title = _hourArray[row];
            }
            if (component==2) {
                title = _minuteArray[row];
            }
            break;
        case unDateStyleShow:{
            NSArray <ResponseSelectionOption*>*selectArr = self.unDateArray[component];
            title = selectArr[row].name;
        }
            break;
        default:
            title = @"";
            break;
    }
    
    customLabel.text = title;
    if (!_datePickerColor) {
        _datePickerColor = [UIColor blackColor];
    }
    customLabel.textColor = _datePickerColor;
    return customLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
 
    if (self.isShowSpeciDate) {
         if (component == 0) {
           yearIndex = row;
           _yearstring = [_yearArray objectAtIndex:yearIndex];
           [self getMonthsArray];
       }
       else if (component == 1){
           monthIndex = row;
           _monthstring = [_monthArray objectAtIndex:monthIndex];
           [self getDaysArray];
       }
       else if (component == 2){
           dayIndex = row;
           _daystring = [_dayArray objectAtIndex:dayIndex];
//               [self getHoursArray];
       }
       else if (component == 3){
           hourIndex = row;
           _hourstring = [_hourArray objectAtIndex:hourIndex];
           [self getMinitsArray];
       }
       else if (component == 4){
           minuteIndex = row;
           _minitstring = [_minuteArray objectAtIndex:minuteIndex];
       }
       [self.datePicker reloadAllComponents];
        NSString *dateStr;
        switch (self.datePickerStyle) {
            case DateStyleShowYearMonthDay:
                dateStr = [NSString stringWithFormat:@"%@-%@-%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex]];
                break;
            case DateStyleShowYearMonth:
                dateStr = [NSString stringWithFormat:@"%@-%@",_yearArray[yearIndex],_monthArray[monthIndex]];
                break;
            case DateStyleShowMonthDay:
                dateStr = [NSString stringWithFormat:@"%@-%@-%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex]];
                break;
            case DateStyleShowHourMinute:
                dateStr = [NSString stringWithFormat:@"%@:%@",_hourArray[hourIndex],_minuteArray[minuteIndex]];
                break;
            case DateStyleShowYear:
                dateStr = [NSString stringWithFormat:@"%@",_yearArray[yearIndex]];
                
                break;
            case DateStyleShowMonth:
                dateStr = [NSString stringWithFormat:@"%@",_monthArray[monthIndex]];
                
                break;
            case DateStyleShowDayHourMinute:
                dateStr = [NSString stringWithFormat:@"%@ %@:%@",_dayArray[dayIndex],_hourArray[hourIndex],_minuteArray[minuteIndex]];
                
                break;
            default:
                if (self.datePickerStyle != unDateStyleShow) {
                    dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex],_hourArray[hourIndex],_minuteArray[minuteIndex]];
                }
                break;
        }
        self.scrollToDate = [[NSDate date:dateStr WithFormat:_dateFormatter] dateWithFormatter:_dateFormatter];
        _startDate = self.scrollToDate;
    }else{
        switch (self.datePickerStyle) {
            case DateStyleShowYearMonthDayHourMinute:{
                if (component == 0) {
                    yearIndex = row;
                }
                if (component == 1) {
                    monthIndex = row;
                }
                if (component == 2) {
                    dayIndex = row;
                }
                if (component == 3) {
                    hourIndex = row;
                }
                if (component == 4) {
                    minuteIndex = row;
                }
                if (component == 0 || component == 1){
                    [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
                    if (_dayArray.count-1<dayIndex) {
                        dayIndex = _dayArray.count-1;
                    }
                }
            }
                break;
            case DateStyleShowYearMonthDay:{
                if (component == 0) {
                    yearIndex = row;
                }
                if (component == 1) {
                    monthIndex = row;
                }
                if (component == 2) {
                    dayIndex = row;
                }
                if (component == 0 || component == 1){
                    [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
                    if (_dayArray.count-1<dayIndex) {
                        dayIndex = _dayArray.count-1;
                    }
                }
            }
                break;
            case DateStyleShowYearMonth:{
                if (component == 0) {
                    yearIndex = row;
                    NSLog(@"yearIndex = %ld",row);
                }
                if (component == 1) {
                    monthIndex = row;
                    NSLog(@"monthIndex = %ld",row);
                }
            }
                break;
            case DateStyleShowMonthDayHourMinute:{
                if (component == 1) {
                    dayIndex = row;
                }
                if (component == 2) {
                    hourIndex = row;
                }
                if (component == 3) {
                    minuteIndex = row;
                }
                if (component == 0) {
                    [self yearChange:row];
                    [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
                    if (_dayArray.count-1<dayIndex) {
                        dayIndex = _dayArray.count-1;
                    }
                }
                [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
                
            }
                break;
            case DateStyleShowMonthDay:{
                if (component == 1) {
                    dayIndex = row;
                }
                if (component == 0) {
                    [self yearChange:row];
                    [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
                    if (_dayArray.count-1<dayIndex) {
                        dayIndex = _dayArray.count-1;
                    }
                }
                [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
            }
                break;
            case DateStyleShowHourMinute:{
                if (component == 0) {
                    hourIndex = row;
                }
                if (component == 1) {
                    minuteIndex = row;
                }
            }
                break;
            case DateStyleShowYear:{
                if (component == 0) {
                    yearIndex = row;
                }
            }
                break;
            case DateStyleShowMonth:{
                if (component == 0) {
                    monthIndex = row;
                }
            }
                break;
            case DateStyleShowDayHourMinute:{
                if (component == 0) {
                    dayIndex = row;
                }
                if (component == 1) {
                    hourIndex = row;
                }
                if (component == 2) {
                    minuteIndex = row;
                }
            }
                break;
            case unDateStyleShow:{
                NSArray <ResponseSelectionOption*>*selectArr = self.unDateArray[component];
                if (self.scrollToSelected.count > 0 && self.scrollToSelected.count == self.unDateArray.count) {
                    [self.scrollToSelected replaceObjectAtIndex:component withObject:selectArr[row]];
                }else{
                    if (self.scrollToSelected.count > 0) {
                        if (component >= self.scrollToSelected.count) {
                            [self.scrollToSelected addObject:selectArr[row]];
                        }
                        [self.scrollToSelected replaceObjectAtIndex:component withObject:selectArr[row]];
                    }else{
                        [self.unDateSelectArr replaceObjectAtIndex:component withObject:selectArr[row]];
                    }
                }
            }
                break;
            default:
                break;
        }
        
        [pickerView reloadAllComponents];
        
        NSString *dateStr;
        if (self.datePickerStyle != unDateStyleShow) {
            dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex],_hourArray[hourIndex],_minuteArray[minuteIndex]];
        }
        switch (self.datePickerStyle) {
            case DateStyleShowYearMonthDay:
                dateStr = [NSString stringWithFormat:@"%@-%@-%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex]];
                break;
            case DateStyleShowYearMonth:
                dateStr = [NSString stringWithFormat:@"%@-%@",_yearArray[yearIndex],_monthArray[monthIndex]];
                break;
            case DateStyleShowMonthDay:
                dateStr = [NSString stringWithFormat:@"%@-%@-%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex]];
                break;
            case DateStyleShowHourMinute:
                dateStr = [NSString stringWithFormat:@"%@:%@",_hourArray[hourIndex],_minuteArray[minuteIndex]];
                break;
            case DateStyleShowYear:
                dateStr = [NSString stringWithFormat:@"%@",_yearArray[yearIndex]];
                
                break;
            case DateStyleShowMonth:
                dateStr = [NSString stringWithFormat:@"%@",_monthArray[monthIndex]];
                
                break;
            case DateStyleShowDayHourMinute:
                dateStr = [NSString stringWithFormat:@"%@ %@:%@",_dayArray[dayIndex],_hourArray[hourIndex],_minuteArray[minuteIndex]];
                
                break;
            default:
                if (self.datePickerStyle != unDateStyleShow) {
                    dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex],_hourArray[hourIndex],_minuteArray[minuteIndex]];
                }
                break;
        }
        
        if (self.datePickerStyle != unDateStyleShow) {
            if ([dateStr containsString:@"至今"]) {
                self.scrollToDate = [[NSDate date] dateWithFormatter:_dateFormatter];
            }else{
                self.scrollToDate = [[NSDate date:dateStr WithFormat:_dateFormatter] dateWithFormatter:_dateFormatter];
            }
            
            if ([self.scrollToDate compare:self.minLimitDate] == NSOrderedAscending) {
                self.scrollToDate = self.minLimitDate;
                [self getNowDate:self.minLimitDate animated:YES];
            }else if ([self.scrollToDate compare:self.maxLimitDate] == NSOrderedDescending){
                self.scrollToDate = self.maxLimitDate;
                [self getNowDate:self.maxLimitDate animated:YES];
            }
            _startDate = self.scrollToDate;
        }else{
            //        [self getNowSelectIndex];
        }
    }
}

-(void)yearChange:(NSInteger)row {
    
    monthIndex = row%12;
    
    //    //年份状态变化
    //    if (row-preRow <12 && row-preRow>0 && [_monthArray[monthIndex] integerValue] < [_monthArray[preRow%12] integerValue]) {
    //        yearIndex ++;
    //    } else if(preRow-row <12 && preRow-row > 0 && [_monthArray[monthIndex] integerValue] > [_monthArray[preRow%12] integerValue]) {
    //        yearIndex --;
    //    }else {
    //        NSInteger interval = (row-preRow)/12;
    //        yearIndex += interval;
    //    }
    
    preRow = row;
}


#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if( [touch.view isDescendantOfView:self.buttomView]) {
        return NO;
    }
    return YES;
}



#pragma mark - Action
-(void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.5f animations:^{
        self.bottomConstraint.constant = 0;
        self.backgroundColor = CREATCOLOR(0, 0, 0, 0.4);
        [self layoutIfNeeded];
        if (self.datePickerStyle == unDateStyleShow) {
            [self getNowSelectIndex];
        }
    }];
}
-(void)dismiss {
    [UIView animateWithDuration:0.5f animations:^{
        self.backgroundColor = CREATCOLOR(0, 0, 0, 0);
        self.bottomConstraint.constant = -310;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        //        [self.unDateSelectArr removeAllObjects];
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}




- (IBAction)doneAction:(UIButton *)btn {
    if (self.datePickerStyle != unDateStyleShow) {
        _startDate = [self.scrollToDate dateWithFormatter:_dateFormatter];
        self.doneBlock(_startDate);
    }else{
        
        if (self.scrollToSelected.count > 0 && self.scrollToSelected.count == self.unDateArray.count) {
            self.unDatedoneBlock(self.scrollToSelected);
        }else{
            if (self.scrollToSelected.count > 0) {
                [self.unDateSelectArr removeAllObjects];
                self.unDateSelectArr = self.scrollToSelected;
            }
            [self.unDateSelectArr removeObject:@""];
            self.unDatedoneBlock(self.unDateSelectArr);
        }
    }
    
    [self dismiss];
}

- (IBAction)cancelAction:(UIButton *)sender {
    [self dismiss];
}

///点击长期按钮
- (IBAction)longTimeClickAction:(UIButton *)sender {
    if (self.selectLongTimeBtnBlock) {
        self.selectLongTimeBtnBlock();
    }
    [self dismiss];
}

#pragma mark - tools
//通过年月求每月天数
- (NSInteger)DaysfromYear:(NSInteger)year andMonth:(NSInteger)month
{
    NSInteger num_year  = year;
    NSInteger num_month = month;
    
    BOOL isrunNian = num_year%4==0 ? (num_year%100==0? (num_year%400==0?YES:NO):YES):NO;
    switch (num_month) {
        case 1:case 3:case 5:case 7:case 8:case 10:case 12:{
            [self setdayArray:31];
            return 31;
        }
        case 4:case 6:case 9:case 11:{
            [self setdayArray:30];
            return 30;
        }
        case 2:{
            if (isrunNian) {
                [self setdayArray:29];
                return 29;
            }else{
                [self setdayArray:28];
                return 28;
            }
        }
        default:
            break;
    }
    return 0;
}

//设置每月的天数数组
- (void)setdayArray:(NSInteger)num
{
    [_dayArray removeAllObjects];
    for (int i=1; i<=num; i++) {
        [_dayArray addObject:[NSString stringWithFormat:@"%02d",i]];
    }
}

//滚动到指定的时间位置
- (void)getNowDate:(NSDate *)date animated:(BOOL)animated
{
    if (!date) {
        date = [NSDate date];
    }
    
    [self DaysfromYear:date.year andMonth:date.month];
    
    yearIndex = date.year-_minYear;
    monthIndex = date.month-1;
    dayIndex = date.day-1;
    hourIndex = date.hour;
    minuteIndex = date.minute;
    
    //循环滚动时需要用到
    preRow = (self.scrollToDate.year-_maxYear)*12+self.scrollToDate.month-1;
    
    NSArray *indexArray;
    
    if (self.datePickerStyle == DateStyleShowYearMonthDayHourMinute)
        indexArray = @[@(yearIndex),@(monthIndex),@(dayIndex),@(hourIndex),@(minuteIndex)];
    if (self.datePickerStyle == DateStyleShowYearMonthDay)
        indexArray = @[@(yearIndex),@(monthIndex),@(dayIndex)];
    if (self.datePickerStyle == DateStyleShowYearMonth)
        indexArray = @[@(yearIndex),@(monthIndex)];
    if (self.datePickerStyle == DateStyleShowMonthDayHourMinute)
        indexArray = @[@(monthIndex),@(dayIndex),@(hourIndex),@(minuteIndex)];
    if (self.datePickerStyle == DateStyleShowMonthDay)
        indexArray = @[@(monthIndex),@(dayIndex)];
    if (self.datePickerStyle == DateStyleShowHourMinute)
        indexArray = @[@(hourIndex),@(minuteIndex)];
    if (self.datePickerStyle == DateStyleShowYear)
        indexArray = @[@(yearIndex)];
    if (self.datePickerStyle == DateStyleShowMonth)
        indexArray = @[@(monthIndex)];
    if (self.datePickerStyle == DateStyleShowDayHourMinute)
        indexArray = @[@(dayIndex),@(hourIndex),@(minuteIndex)];
    
    [self.datePicker reloadAllComponents];
    
    for (int i=0; i<indexArray.count; i++) {
        if ((self.datePickerStyle == DateStyleShowMonthDayHourMinute || self.datePickerStyle == DateStyleShowMonthDay)&& i==0) {
            NSInteger mIndex = [indexArray[i] integerValue]+(12*(self.scrollToDate.year - _minYear));
            [self.datePicker selectRow:mIndex inComponent:i animated:animated];
        } else {
            [self.datePicker selectRow:[indexArray[i] integerValue] inComponent:i animated:animated];
        }
        
    }
}

//滚动到指定的时间位置
- (void)getNowSelectIndex
{
    if (self.scrollToSelected.count != 0) {
        for (int i = 0; i<self.scrollToSelected.count; i++) {
            NSArray <ResponseSelectionOption *>*array = self.unDateArray[i];
            for (int j = 0; j< array.count; j++) {
                if (self.scrollToSelected[i] == array[j]) {
                    [self.datePicker selectRow:j inComponent:i animated:YES];
                }
            }
        }
    }
    
    
}

#pragma mark - getter / setter
-(UIPickerView *)datePicker {
    if (!_datePicker) {
        [self.buttomView layoutIfNeeded];
        _datePicker = [[UIPickerView alloc] init];
        _datePicker.showsSelectionIndicator = YES;
        _datePicker.delegate = self;
        _datePicker.dataSource = self;
    }
    return _datePicker;
}

- (void)setPickerViewTitle:(NSString *)pickerViewTitle{
    _pickerViewTitle = pickerViewTitle;
    self.statusTitle.text = _pickerViewTitle;
}

- (void)setShowLongTime:(BOOL)showLongTime {
    _showLongTime = showLongTime;
    _showLongTime == YES ? self.longTimeView.hidden = NO : YES;
}

- (void)setShowToNow:(BOOL)showToNow{
    _showToNow = showToNow;
    if (_showToNow) {
        [_yearArray addObject:@"至今"];
    }
}

- (void)setLongTimeTitleLab:(UILabel *)longTimeTitleLab{
    _longTimeTitleLab = longTimeTitleLab;
    [self.longTimeButton setTitle:_longTimeTitleLab.text forState:UIControlStateNormal];
    [self.longTimeButton setTitleColor:_longTimeTitleLab.textColor forState:UIControlStateNormal];
}

-(void)setMinLimitDate:(NSDate *)minLimitDate {
    _minLimitDate = minLimitDate;
    if ([_scrollToDate compare:self.minLimitDate] == NSOrderedAscending) {
        _scrollToDate = self.minLimitDate;
    }
    [self getNowDate:self.scrollToDate animated:NO];
}

-(void)setDoneButtonColor:(UIColor *)doneButtonColor {
    _doneButtonColor = doneButtonColor;
    [self.doneBtn setTitleColor:doneButtonColor forState:UIControlStateNormal];
}

-(void)setHideBackgroundYearLabel:(BOOL)hideBackgroundYearLabel {
}

- (NSMutableArray *)unDateSelectArr
{
    if (!_unDateSelectArr) {
        _unDateSelectArr = [NSMutableArray arrayWithCapacity:self.unDateArray.count];
        for (int i = 0; i < self.unDateArray.count; i++) {
            [_unDateSelectArr addObject:@""];
        }
    }
    return _unDateSelectArr;
}

- (NSMutableArray<ResponseSelectionOption *> *)scrollToSelected{
    if (!_scrollToSelected) {
        _scrollToSelected = [NSMutableArray array];
    }
    return _scrollToSelected;
}

-(void)getYearsArray{
    
    int yearIntmin = [_minDate getYear];
    int yearIntmax = [_maxDate getYear];
    [_yearArray removeAllObjects];
    for (int i=yearIntmin; i<=yearIntmax; i++) {
        [_yearArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
//    _yearstring = [_yearArray objectAtIndex:0];
//    [self.datePicker reloadComponent:0];
//    [self.datePicker selectRow:0 inComponent:0 animated:NO];
//    [self getMonthsArray];
}
-(void)getMonthsArray{
    
    int monthmin = [_minDate getMonth];
    int monthmax = [_maxDate getMonth];
    int yl = 1;
    int yr = 12;
    
    if (yearIndex == 0) {
        yl = monthmin;
    }
    if (yearIndex == _yearArray.count-1) {
        yr = monthmax;
    }
    [_monthArray removeAllObjects];
    for (int i=yl; i<=yr; i++) {
        NSString *num = [NSString stringWithFormat:@"%02d",i];
        [_monthArray addObject:num];
    }
    int c = [_monthstring intValue];
    int x = [[_monthArray objectAtIndex:0] intValue];
    int d = [[_monthArray objectAtIndex:_monthArray.count-1] intValue];
    if (c<=x) {
        monthIndex = 0;
    }
    else if (c>=d){
        monthIndex = _monthArray.count-1;
    }
    else if (c>x && c<d){
        monthIndex = c-x;
    }
    _monthstring = [_monthArray objectAtIndex:monthIndex];
    [self.datePicker reloadComponent:1];
    [self.datePicker selectRow:monthIndex inComponent:1 animated:NO];
    [self getDaysArray];
}
-(void)getDaysArray{
    int monthmin = [_minDate getDay];
    int monthmax = [_maxDate getDay];
    int daycout = [self getCurrentAllDay:[[_yearArray objectAtIndex:yearIndex] intValue] andmonth:[[_monthArray objectAtIndex:monthIndex] intValue]];
    int yl = 1;
    int yr = daycout;
    
    if (yearIndex == 0 && monthIndex == 0) {
        yl = monthmin;
    }
    if (yearIndex == _yearArray.count-1 && monthIndex == _monthArray.count-1) {
        yr = monthmax;
    }
    [_dayArray removeAllObjects];
    for (int i=yl; i<=yr; i++) {
        [_dayArray addObject:[NSString stringWithFormat:@"%02d",i]];
//        [_dayArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    //28 31  cout4    30  27 32  30
    
    int c = [_daystring intValue];
    int x = [[_dayArray objectAtIndex:0] intValue];
    int d = [[_dayArray objectAtIndex:_dayArray.count-1] intValue];
    if (c<=x) {
        dayIndex = 0;
    }
    else if (c>=d){
        dayIndex = _dayArray.count-1;
    }
    else if (c>x && c<d){
        dayIndex = c-x;
    }
    _daystring = [_dayArray objectAtIndex:dayIndex];
    [self.datePicker reloadComponent:2];
    [self.datePicker selectRow:dayIndex inComponent:2 animated:NO];
//    [self getHoursArray];
    
}
-(void)getHoursArray{
    int monthmin = [_minDate getHour];
    int monthmax = [_maxDate getHour];
    int yl = 0;
    int yr = 23;
    
    if (yearIndex == 0 && monthIndex == 0 && dayIndex == 0) {
        yl = monthmin;
    }
    if (yearIndex == _yearArray.count-1 && monthIndex == _monthArray.count-1 && dayIndex == _dayArray.count-1) {
        yr = monthmax;
    }
    [_hourArray removeAllObjects];
    for (int i=yl; i<=yr; i++) {
        [_hourArray addObject:[NSString stringWithFormat:@"%02d",i]];
    }
    int c = [_hourstring intValue];
    int x = [[_hourArray objectAtIndex:0] intValue];
    int d = [[_hourArray objectAtIndex:_hourArray.count-1] intValue];
    if (c<=x) {
        hourIndex = 0;
    }
    else if (c>=d){
        hourIndex = _hourArray.count-1;
    }
    else if (c>x && c<d){
        hourIndex = c-x;
    }
    _hourstring = [_hourArray objectAtIndex:hourIndex];
    [self.datePicker reloadComponent:3];
    [self.datePicker selectRow:hourIndex inComponent:3 animated:NO];
    [self getMinitsArray];
}
-(void)getMinitsArray{
    int monthmin = [_minDate getMinute];
    int monthmax = [_maxDate getMinute];
    int yl = 0;
    int yr = 59;
    
    if (yearIndex == 0 && monthIndex == 0 && dayIndex == 0 && hourIndex == 0) {
        yl = monthmin;
    }
    if (yearIndex == _yearArray.count-1 && monthIndex == _monthArray.count-1 && dayIndex == _dayArray.count-1 && hourIndex == _hourArray.count-1) {
        yr = monthmax;
    }
    [_minuteArray removeAllObjects];
    for (int i=yl; i<=yr; i++) {
        [_minuteArray addObject:[NSString stringWithFormat:@"%02d",i]];
    }
    int c = [_minitstring intValue];
    int x = [[_minuteArray objectAtIndex:0] intValue];
    int d = [[_minuteArray objectAtIndex:_minuteArray.count-1] intValue];
    if (c<=x) {
        minuteIndex = 0;
    }
    else if (c>=d){
        minuteIndex = _minuteArray.count-1;
    }
    else if (c>x && c<d){
        minuteIndex = c-x;
    }
    _minitstring = [_minuteArray objectAtIndex:minuteIndex];
    [self.datePicker reloadComponent:4];
    [self.datePicker selectRow:minuteIndex inComponent:4 animated:NO];
}

-(int)getCurrentAllDay:(int)year andmonth:(int)month{
    
    int endDate = 0;
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            endDate = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            endDate = 30;
            break;
        case 2:
            // 是否为闰年
            if (year % 400 == 0) {
                endDate = 29;
            } else {
                if (year % 100 != 0 && year %4 ==4) {
                    endDate = 29;
                } else {
                    endDate = 28;
                }
            }
            break;
        default:
            break;
    }
    
    return endDate;
}
@end

